class CulturalHeritage::PreRegisterRequestsController < ApplicationController
  before_filter :authenticate_user!
  
  # GET /cultural_heritage/pre_register_requests
  # GET /cultural_heritage/pre_register_requests.xml
  #
  def index
    if (user_signed_in? && ([7,2].include?(current_user.role)))
      @title_view = 'Solicitud de Pre Registro'
      @cultural_heritage_pre_register_requests = CulturalHeritage::PreRegisterRequest.find(:all,:conditions=>['deleted != ? or deleted is NULL',1])
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @cultural_heritage_pre_register_requests }
      end
    else
      respond_to do |format|
      format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/pre_register_requests/1
  # GET /cultural_heritage/pre_register_requests/1.xml
  def show
    if (user_signed_in? && ([1,2,7].include?(current_user.role)))
      @title_view = 'Solicitud de Pre Registro'
      @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])
      @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
      @list_pre_registers = CulturalHeritage::CulturalHeritagePreRegister.find(:all,:conditions=>['cultural_heritage_pre_register_request_id = :id and (deleted != :deleted or deleted is NULL)',{:id=>params[:id].to_i, :deleted=>1}])
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @cultural_heritage_pre_register_request }
      end
     else
      respond_to do |format|
      format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/pre_register_requests/new
  # GET /cultural_heritage/pre_register_requests/new.xml
  def new
    if (user_signed_in? && ([1,2,3,4,7].include?(current_user.role)))
    #Cargamos la vista inicial
      @title_view = 'Solicitud de Pre Registro'
      @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.new(params[:cultural_heritage_pre_register_request])
      @cultural_heritage_states = CulturalHeritage::State.all
      @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_pre_register_request }
      end
    else
      respond_to do |format|
      format.html { redirect_to(new_user_session_path) }
      end
    end 
  end

  # GET /cultural_heritage/pre_register_requests/1/edit
  def edit
    @title_view = 'Solicitud de Pre Registro'
    @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])
    @cultural_heritage_states = CulturalHeritage::State.all

    @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})

    @list_pre_registers = CulturalHeritage::CulturalHeritagePreRegister.find(:all,:conditions=>['cultural_heritage_pre_register_request_id = :id and (deleted != :deleted or deleted is NULL)',{:id=>params[:id].to_i, :deleted=>1}])



    end

  #Funcion que recibe el nombre del parametro y el nuevo valor y lo actualizamos en BD
  def change_attribute
    field = params[:field]
    new_value = params[:new_value]
    cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])
    attrs = cultural_heritage_pre_register_request.attributes
    attrs[field] = new_value
    cultural_heritage_pre_register_request.attributes = attrs
    cultural_heritage_pre_register_request.save
    render :text=>"", :layout=>false
  end

  # POST /cultural_heritage/pre_register_requests
  # POST /cultural_heritage/pre_register_requests.xml
  def create
    @title_view = 'Solicitud de Pre Registro'
    #Aqui en el create no guardamos sino con los parametros, mostramos el siguiente paso
    @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.new(params[:cultural_heritage_pre_register_request])
    @cultural_heritage_pre_register_request.user = current_user.id

    @cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new()
    @list_pre_registers = []

    @cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new()
    @lista_instituciones = CulturalHeritage::CulturalInstitution.all
    @cultural_heritage_pre_register.state = 5
#    @cultural_heritage_states = CulturalHeritage::State.all
    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_periods = CulturalHeritage::Period.all
    @cultural_heritage_cultures = CulturalHeritage::Culture.all
    @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
    @cultural_heritage_styles = CulturalHeritage::Style.all
    @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
    @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
    @cultural_heritage_shapes = CulturalHeritage::Shape.all
    @cultural_heritage_techniques = CulturalHeritage::Technique.all
    @cultural_heritage_topics = CulturalHeritage::Topic.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_groups = CulturalHeritage::Group.all
    @cultural_heritage_subgroups = CulturalHeritage::SubGroup.all
    

    respond_to do |format|
      format.html { render :action=>'step_pre_register',
                           :cultural_heritage_pre_register_request=>@cultural_heritage_pre_register_request.attributes
                  }
    end

  end

  def step_pre_register

    @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])
    @title_view = 'Solicitud de Pre Registro'
    @list_pre_registers = []
     @typologies = []
    @cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new()
    @lista_instituciones = CulturalHeritage::CulturalInstitution.all
    @cultural_heritage_states = CulturalHeritage::State.all
    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_periods = CulturalHeritage::Period.all
    @cultural_heritage_cultures = CulturalHeritage::Culture.all
    @cultural_heritage_styles = CulturalHeritage::Style.all
    @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
    @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
    @cultural_heritage_shapes = CulturalHeritage::Shape.all
    @cultural_heritage_techniques = CulturalHeritage::Technique.all
    @cultural_heritage_topics = CulturalHeritage::Topic.all
    @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_groups = CulturalHeritage::Group.all
    @cultural_heritage_subgroups = CulturalHeritage::SubGroup.all
  end

  def process_step_pre_register
     @title_view = 'Solicitud de Pre Registro'
    if params[:commit].to_s == "Guardar Otro"
      #Si es Guardar otro, guardamos los campos del formulario de pre-register en un objeto y lo agregamos a nuestra lista de pre-registros agregados
      if params[:request_pre_register_id].blank?
        @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.new(params[:cultural_heritage_pre_register_request])
      else
        @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:request_pre_register_id])
      end
      list_pre_registers = params[:list_pre_registers].to_a
      @list_pre_registers = []
      list_pre_registers.each do |pre_register|
        @list_pre_registers << CulturalHeritage::CulturalHeritagePreRegister.new(pre_register)
      end

      @list_pre_registers << CulturalHeritage::CulturalHeritagePreRegister.new(params[:cultural_heritage_cultural_heritage_pre_register])

      @cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new()
      @lista_instituciones = CulturalHeritage::CulturalInstitution.all
      @cultural_heritage_pre_register.state = 5
#      @cultural_heritage_states = CulturalHeritage::State.all
      @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
      @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
      @cultural_heritage_periods = CulturalHeritage::Period.all

      @cultural_heritage_cultures = CulturalHeritage::Culture.all
      @cultural_heritage_styles = CulturalHeritage::Style.all
      @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
      @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
      @cultural_heritage_shapes = CulturalHeritage::Shape.all
      @cultural_heritage_techniques = CulturalHeritage::Technique.all
      @cultural_heritage_topics = CulturalHeritage::Topic.all
      @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
      @cultural_heritage_groups = CulturalHeritage::Group.all
      @cultural_heritage_subgroups = CulturalHeritage::SubGroup.all
      @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})

      list_pre_registers_attr = params[:list_pre_registers].to_a
      list_pre_registers_attr << params[:cultural_heritage_cultural_heritage_pre_register]
      respond_to do |format|
        format.html { render :action=>'step_pre_register',
                             :cultural_heritage_pre_register_request=>@cultural_heritage_pre_register_request.attributes,
                             :list_pre_registers=>list_pre_registers_attr
                    }
      end
    else
      if params[:request_pre_register_id].blank?
        cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.new(params[:cultural_heritage_pre_register_request])
      else
        cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:request_pre_register_id])
      end
      #Si ya es hora de guardar, recorremos la lista guardando cada uno de los pre-registros, despues de registrar la solicitud
      
      if cultural_heritage_pre_register_request.save
        list_pre_registers_attr = params[:list_pre_registers].to_a
        list_pre_registers_attr << params[:cultural_heritage_cultural_heritage_pre_register]

        list_pre_registers_attr.each do |pre_register_attr|
          obj_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new(pre_register_attr)
          obj_pre_register.cultural_heritage_pre_register_request_id = cultural_heritage_pre_register_request.id
          obj_pre_register.save
        end
      end
      redirect_to :action=>"index"
    end
  end

  def edit_pre_register
    @title_view = 'Solicitud de Pre Registro'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
    @lista_instituciones = CulturalHeritage::CulturalInstitution.all
    @cultural_heritage_states = CulturalHeritage::State.all
    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_periods = CulturalHeritage::Period.all
    @cultural_heritage_cultures = CulturalHeritage::Culture.all
    @cultural_heritage_styles = CulturalHeritage::Style.all
    @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
    @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
    @cultural_heritage_shapes = CulturalHeritage::Shape.all
    @cultural_heritage_techniques = CulturalHeritage::Technique.all
    @cultural_heritage_topics = CulturalHeritage::Topic.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_groups = CulturalHeritage::Group.all
    @cultural_heritage_subgroups = CulturalHeritage::SubGroup.all
    @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
  end

  def show_pre_register
    @title_view = 'Solicitud de Pre Registro'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
#    if @cultural_heritage_cultural_heritage_pre_register.father_maximum_id != -1
#      @father_node   = CulturalHeritage::CulturalHeritage.find(@cultural_heritage_cultural_heritage.father_maximum_id)
#    else
#      #aca funciona
#      @father_node = @cultural_heritage_cultural_heritage
#    end
#    @cultural_heritage_cultural_heritage_models = @cultural_heritage_cultural_heritage.list_models
#    @cultural_heritage_cultural_heritage_researches = @cultural_heritage_cultural_heritage.list_researches
#    @cultural_heritage_cultural_heritage_avatars = @cultural_heritage_cultural_heritage.list_avatars
  if !(@cultural_heritage_cultural_heritage_pre_register.culturalInstitution.blank?)    
    if (@cultural_heritage_cultural_heritage_pre_register.gmaps == true) or (@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.gmaps == true)
      @json = "["
      if (@cultural_heritage_cultural_heritage_pre_register.gmaps == true)
        @json = @json << '{"description":"'+@cultural_heritage_cultural_heritage_pre_register.define_info_window_for_marker_for_maps(root_url)+'","title":'+'"'+@cultural_heritage_cultural_heritage_pre_register.name+'","sidebar":"Lugar de Hallazgo","lat":"'+@cultural_heritage_cultural_heritage_pre_register.latitude.to_s+'","lng":"'+@cultural_heritage_cultural_heritage_pre_register.longitude.to_s+'"}'
      end
      if (@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.gmaps == true)
        if (@cultural_heritage_cultural_heritage_pre_register.gmaps == true)
          @json = @json << ','
        end
        @json = @json << '{"description":"'+@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.define_info_window_for_marker_for_maps(root_url)+'","title":'+'"'+@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.name+'","sidebar":"Ubicacion Actual","lat":"'+@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.latitude.to_s+'","lng":"'+@cultural_heritage_cultural_heritage_pre_register.obj_culturalInstitution.longitude.to_s+'"}'
      end
      @json = @json << "]"
    end
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register }
      format.json  { render :json => @cultural_heritage_cultural_heritage_pre_register }
    end
#    @title_view = 'Solicitud de Pre Registro'
#    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
#    @lista_instituciones = CulturalHeritage::CulturalInstitution.all
#    @cultural_heritage_states = CulturalHeritage::State.all
#    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
#    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
#    @cultural_heritage_periods = CulturalHeritage::Period.all
#    @cultural_heritage_cultures = CulturalHeritage::Culture.all
#    @cultural_heritage_styles = CulturalHeritage::Style.all
#    @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
#    @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
#    @cultural_heritage_shapes = CulturalHeritage::Shape.all
#    @cultural_heritage_techniques = CulturalHeritage::Technique.all
#    @cultural_heritage_topics = CulturalHeritage::Topic.all
#    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
#    @cultural_heritage_groups = CulturalHeritage::Group.all
#    @cultural_heritage_subgroups = CulturalHeritage::SubGroup.all
#    @people = Security::Person.find(:all,:conditions=>{:deleted=>[0]})
  end

  #DELETE SON
  def delete_pre_register
    @title_view = 'Solicitud de Pre Registro'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
    @cultural_heritage_cultural_heritage_pre_register.deleted = 1
    @cultural_heritage_cultural_heritage_pre_register.save

    respond_to do |format|
      format.html { redirect_to({:action=>"edit",:id=>@cultural_heritage_cultural_heritage_pre_register.cultural_heritage_pre_register_request_id}) }
      format.xml  { head :ok }
    end
  end
  
  # PUT /cultural_heritage/pre_register_requests/1
  # PUT /cultural_heritage/pre_register_requests/1.xml
  def update
    @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])

    respond_to do |format|
      if @cultural_heritage_pre_register_request.update_attributes(params[:cultural_heritage_pre_register_request])
        format.html { redirect_to(@cultural_heritage_pre_register_request, :notice => 'Pre register request was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @cultural_heritage_pre_register_request.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /cultural_heritage/pre_register_requests/1
  # DELETE /cultural_heritage/pre_register_requests/1.xml
  def destroy
    @cultural_heritage_pre_register_request = CulturalHeritage::PreRegisterRequest.find(params[:id])
    @cultural_heritage_pre_register_request.deleted = 1
    @cultural_heritage_pre_register_request.save

    respond_to do |format|
      format.html { redirect_to(cultural_heritage_pre_register_requests_url) }
      format.xml  { head :ok }
    end
  end




  def update_table

      @search_input = params[:search_id].to_s
      @search_field = params[:field_name].to_s
      @search_state = params[:search_request_state].to_s

      @cultural_heritage_pre_register_requests = nil

        case @search_field
          when "Todos" then @variable= "cultural_heritage_pre_register_requests.id LIKE ? or cultural_heritage_pre_register_requests.description LIKE ? and "
          when "Descripcion" then @cultural_heritage_pre_register_requests = CulturalHeritage::PreRegisterRequest.find(:all,:conditions=>["cultural_heritage_pre_register_request.description LIKE ? and cultural_heritage_pre_register_request.state = 0 ",'%'+@search_input+'%'])
          when "Codigo" then @cultural_heritage_pre_register_requests = CulturalHeritage::PreRegisterRequest.find(:all,:conditions=>["cultural_heritage_pre_register_request.id LIKE ? and cultural_heritage_pre_register_request.state = 0 ",'%'+@search_input+'%'])
         end

      if (@search_field=="Todos")
    
#      if ! @search_state.blank?
      
        @cultural_heritage_pre_register_requests = CulturalHeritage::PreRegisterRequest.find(:all,:conditions=>[""+@variable+ "cultural_heritage_pre_register_requests.state = ?",'%'+@search_input+'%','%'+@search_input+'%', @search_state])
#      end

    end
   
render :partial => "table"

  end
end
